﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace myFirstSharePointObjectModel.siteStructure
{
    public partial class siteStructureUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SPFarm thisFarm = SPFarm.Local;
                webAppDropList.Items.Add(new ListItem("select"));
                //Iterate through WebApplications
                foreach (SPService svc in thisFarm.Services)
                {
                    if (svc is SPWebService)
                    {
                        SPWebService webSvc = (SPWebService)svc;

                        foreach (SPWebApplication webApp in webSvc.WebApplications)
                        {

                            if (!webApp.IsAdministrationWebApplication)
                            {
                                webAppDropList.Items.Add(new ListItem(webApp.Name, webApp.Id.ToString()));
                            }
                        }
                    }
                }
            }



        }

        protected void webAppDropList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string webOpId = webAppDropList.SelectedValue;
            SiteCollDropList.Items.Clear();
            if (webOpId != "select")
            {
                SPWebApplication webApp = (SPWebApplication)SPFarm.Local.GetObject(new Guid(webOpId));

                foreach (SPSite siteCollection in webApp.Sites)
                {
                    try
                    {
                        SiteCollDropList.Items.Add(new ListItem(siteCollection.Url, siteCollection.ID.ToString()));
                    }
                    finally
                    {
                        siteCollection.Dispose();
                    }
                }
            }

            {

            }


        }

        protected void SiteCollDropList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string SiteColId = SiteCollDropList.SelectedValue;
            sitesDropList.Items.Clear();
            if (SiteColId != "select")
            {
                using (SPSite siteCollection = new SPSite(new Guid(SiteColId)))
                {
                    foreach (SPWeb site in siteCollection.AllWebs)
                    {
                        try
                        {
                            sitesDropList.Items.Add(new ListItem(site.Title, site.ID.ToString()));
                        }
                        finally
                        {
                            site.Dispose();
                        }
                    }
                }





            }
        }

        protected void sitesDropList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string SiteColId = SiteCollDropList.SelectedValue;
            string SiteId = sitesDropList.SelectedValue;

            listsDropList.Items.Clear();
            if (SiteColId != "select")
            {
                using (SPSite siteCollection = new SPSite(new Guid(SiteColId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(SiteId)))
                    {
                        foreach (SPList list in site.Lists)
                        {
                            listsDropList.Items.Add(new ListItem(list.Title, list.ID.ToString()));
                        }
                    }
                }







            }
        }

        protected void listsDropList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string SiteColId = SiteCollDropList.SelectedValue;
            string SiteId = sitesDropList.SelectedValue;
            string ListId = listsDropList.SelectedValue;
            itemsDropList.Items.Clear();
            if (SiteColId != "select")
            {
                using (SPSite siteCollection = new SPSite(new Guid(SiteColId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(SiteId)))
                    {
                        SPList list = site.Lists[new Guid(ListId)];
                        foreach (SPListItem listItem in list.Items)
                        {
                            itemsDropList.Items.Add(new ListItem(listItem.Title, listItem.ID.ToString()));


                        }
                    }
                }
            }
        }

        protected void itemsDropList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string SiteColId = SiteCollDropList.SelectedValue;
            string SiteId = sitesDropList.SelectedValue;
            string ListId = listsDropList.SelectedValue;
            string ListItemId = itemsDropList.SelectedValue;
            fieldsDropList.Items.Clear();
            if (SiteColId != "select")
            {
                using (SPSite siteCollection = new SPSite(new Guid(SiteColId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(SiteId)))
                    {
                        SPList list = site.Lists[new Guid(ListId)];
                        SPListItem listItem = list.GetItemById(int.Parse(ListItemId));
                        foreach (SPField field in listItem.Fields)
                        {
                            fieldsDropList.Items.Add(new ListItem(field.Title,field.Id.ToString()));
                        }


                    }
                }
            }
        }

        protected void fieldsDropList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string SiteColId = SiteCollDropList.SelectedValue;
            string SiteId = sitesDropList.SelectedValue;
            string ListId = listsDropList.SelectedValue;
            string ListItemId = itemsDropList.SelectedValue;
            string FieldId = fieldsDropList.SelectedValue;
            
            if (SiteColId != "select")
            {
                using (SPSite siteCollection = new SPSite(new Guid(SiteColId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(SiteId)))
                    {
                        SPList list = site.Lists[new Guid(ListId)];
                        SPListItem listItem = list.GetItemById(int.Parse(ListItemId));
                        SPField field = listItem.Fields[new Guid(FieldId)];
                        String value = listItem[field.Id].ToString();
                        valueTextBox.Text = value;

                    }
                }
            }
        }
    
    }
}